// Copyright (c) 2007-2008 Simon Fell
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
// THE SOFTWARE.
//

#import "Mru.h"
#import "Preferences.h"



@implementation MruItem 

- (id)initWithItem:(id)i {
	self = [super init];
	item = [i retain];
	lastAccess = [[NSDate date] retain];
	[self resetExpiry];
	return self;
}

- (void) dealloc {
	[item release];
	[lastAccess release];
	[super dealloc];
}

- (void)setItem:(id)i {
	@synchronized(self) {
		id oldItem = [self item];
		item = [i retain];
		[oldItem release];
		[self resetExpiry];
	}
}

- (id)item {
	@synchronized(self) {
		[lastAccess release];
		lastAccess = [[NSDate date] retain];
		return item;
	}
	return 0;	// dummy for compiler
}

- (NSDate *)lastAccess {
	@synchronized(self) {
		return lastAccess;
	}
	return 0;	// dummy for compiler
}

- (BOOL)isExpired {
	return expires < [NSDate timeIntervalSinceReferenceDate];
}

- (void)resetExpiry {
	expires = [NSDate timeIntervalSinceReferenceDate] + (60 * [[NSUserDefaults standardUserDefaults] integerForKey:P_DATA_CACHE_TIME]);
}

- (NSComparisonResult)compareLastAccess:(MruItem *)other {
	return [[other lastAccess] compare:[self lastAccess]];
}

@end

@implementation Mru

- (id)init {
	self = [super init];
	items = [[NSMutableDictionary alloc] init];
	return self;
}

- (int)maxItems {
	return [[NSUserDefaults standardUserDefaults] integerForKey:P_DATA_CACHE_ITEMS];
}

- (MruItem *)mruItemForKey:(NSString *)key {
	@synchronized(items) {
		return [items objectForKey:key];
	}
	return 0;	// dummy for compiler
}

- (void)clearOldestItems:(int)numToRemove {
	NSArray *keys = [items keysSortedByValueUsingSelector:@selector(compareLastAccess:)];
	NSString *key;
	NSEnumerator *e = [keys objectEnumerator];
	while ((key = [e nextObject]) && (numToRemove > 0)) {
		[items removeObjectForKey:key];
		numToRemove--;
	}
}

- (void)removeObjectForKey:(NSString *)key {
	@synchronized(self) {
		[items removeObjectForKey:key];
	}
}

- (void)setObject:(id)obj forKey:(NSString *)key {
	@synchronized(self) {
		MruItem *item = [items objectForKey:key];
		if (item != nil) {
			[item setItem:obj];
		} else {
			item = [[MruItem alloc] initWithItem:obj];
			int maxItems = [self maxItems];
			if ([items count] >= maxItems)
				[self clearOldestItems:1 + maxItems - [items count]];
			[items setObject:item forKey:key];
		}
	}
}

@end
